//
//  CasesShowInEffectViewController.m
//  customer
//
//  Created by Holothurian on 2018/10/29.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "CasesShowInEffectViewController.h"
#import "CasesShowCollectionViewCell.h"
#import "CasesShowModel.h"
#import "AppointmentApplyViewController.h"
#import "HSImageScrollViewController.h"

static NSString *ItemIdentifier = @"ItemIdentifier";
@interface CasesShowInEffectViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    UICollectionViewFlowLayout *_flowLayout;
    
}

@property (nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic,strong)NSMutableArray *dataSource;

@property (nonatomic,strong)NSMutableArray *imgUrlArr;

@end

@implementation CasesShowInEffectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"案例效果图";
    _imgUrlArr = [NSMutableArray array];
    [self layoutCollectionView];
    [self loadData];
}

- (void)loadData{
    [self showLoading];
    AutoWeakSelf
    [[QYNetManager sharedManager]POST:kCasesEffectDetail parameters:@{@"effect_id":self.effect_id?:@""} andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [weakSelf hideLoading];
        NSLog(@"%@",responseObject[@"data"]);
        if ([responseObject[@"status"] isEqualToString:@"error"]) {
            [weakSelf showErrorTipWithTitle:responseObject[@"error"]];
        }else{
            NSMutableArray *tempArr = [responseObject[@"data"] objectForKey:@"effectImgBeans"];
            for (NSDictionary *dict in tempArr) {
                CasesShowBaseModel *baseModel = [CasesShowBaseModel yy_modelWithDictionary:dict];
                [weakSelf.dataSource addObject:baseModel];
                [weakSelf.imgUrlArr addObject:baseModel.effect_img];
            }
            [weakSelf.collectionView reloadData];
        }
    } Failure:^(NSError *error) {
        [weakSelf hideLoading];
        [weakSelf showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}


- (void)layoutCollectionView{
    
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _flowLayout.itemSize = CGSizeMake((F_DEVICE_W - 45) / 2.0, F_DEVICE_H / 4.0);
    _flowLayout.minimumLineSpacing = 20;
    _flowLayout.minimumInteritemSpacing = 15;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, F_DEVICE_W, F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(100)) collectionViewLayout: _flowLayout];
    collectionView.bounces = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    [self.collectionView registerClass:[CasesShowCollectionViewCell class] forCellWithReuseIdentifier:ItemIdentifier];
    
    
    UIButton *appointButton = [UIButton buttonWithType:UIButtonTypeCustom];
    appointButton.frame = CGRectMake(30, F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(20), F_DEVICE_W - 60, 50);
    appointButton.layer.cornerRadius = 6;
    appointButton.layer.masksToBounds = YES;
    [appointButton setBackgroundColor:[HSPublic colorWithHexString:@"333333"]];
    [appointButton setImage:[UIImage imageNamed:@"designAppointment"] forState:UIControlStateNormal];
    [appointButton setImage:[UIImage imageNamed:@"designAppointment"] forState:UIControlStateSelected];
    [appointButton setTitle:@"立即预约" forState:UIControlStateNormal];
    [appointButton setTitle:@"立即预约" forState:UIControlStateSelected];
    [appointButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [appointButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [appointButton addTarget:self action:@selector(appointmentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:appointButton];
    
}

- (void)appointmentAction:(UIButton *)sender{
    AppointmentApplyViewController *apply = [[AppointmentApplyViewController alloc]init];
    TAB_PUSH(self, apply, YES);
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.dataSource count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CasesShowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifier forIndexPath:indexPath];
    CasesShowBaseModel *model = self.dataSource[indexPath.row];
    [cell cellForBaseLayout:model WithIndexPath:indexPath];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HSImageScrollViewController *scrollVC = [[HSImageScrollViewController alloc]init];
    scrollVC.imgUrlArr = self.imgUrlArr;
    scrollVC.index = indexPath.row;
    [self presentViewController:scrollVC animated:NO completion:nil];

}

- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
